<template>
  <el-card style="margin-bottom:20px;">
    <div slot="header"
         class="clearfix">
      <span>{{$t('profile.information')|uppercaseFirst}}</span>
    </div>

    <div class="user-profile">
      <div class="box-center">
        <!-- <pan-thumb :image="user.avatar" :height="'100px'" :width="'100px'" :hoverable="false">
          <div>Hello</div>
          {{ user.role }}
        </pan-thumb> -->
      </div>
      <div class="box-center">
        <div class="user-name text-center">{{$t('profile.name')|uppercaseFirst}}: {{ user.name }}</div>
        <div class="user-role text-center text-muted">{{$t('profile.role')|uppercaseFirst}}: {{ user.role | uppercaseFirst }}</div>
      </div>
    </div>

    <div class="user-bio">
      <div class="user-education user-bio-section">
        <div class="user-bio-section-header">
          <svg-icon icon-class="education" /><span>{{$t('common.action')}}</span></div>
        <div class="user-bio-section-body">
          <div class="text-muted">
            <el-button type="primary"
                       @click="handleCreatePassword">{{$t('profile.changePassword')}}</el-button>
          </div>
        </div>
      </div>

      <el-dialog :title="$t('profile.changePassword')"
                 :visible.sync="dialogPasswordVisible">
        <el-form ref="resetForm"
                 :model="resetForm"
                 :rules="resetFormRules"
                 label-position="left">
          <el-form-item :label="$t('profile.oldPassword')"
                        prop="oldPassword"
                        :error="errorPassword">
            <el-input type="password"
                      v-model="resetForm.oldPassword"
                      auto-complete="off" />
          </el-form-item>
          <el-form-item :label="$t('profile.newPassword')"
                        prop="newPassword">
            <el-input type="password"
                      v-model="resetForm.newPassword "
                      auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item :label="$t('profile.confirmNewPassword')"
                        prop="renewPassword">
            <el-input type="password"
                      v-model="resetForm.renewPassword"
                      auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer"
             class="dialog-footer">
          <el-button type="danger"
                     @click="dialogPasswordVisible = false">
            {{$t('common.cancel')}}
          </el-button>
          <el-button type="primary"
                     @click="createPassword">
            {{$t('common.confirm')}}
          </el-button>
        </div>
      </el-dialog>
    </div>
  </el-card>

</template>

<script>
// import PanThumb from '@/components/PanThumb'
import { changePassword } from '@/api/user'
import permission from '@/directive/permission/index.js' // 权限判断指令

export default {
  // components: { PanThumb },
  directives: { permission },

  props: {
    user: {
      type: Object,
      default: () => {
        return {
          name: '',
          email: '',
          avatar: '',
          role: ''
        }
      }
    }
  },
  data() {
    const validateOldPwd = (rule, value, callback) => {
      if (value === undefined) {
        callback(new Error(this.$t('profile.validate.oldPwd.null')))
      } else if (value.toString().length < 5 || value.toString().length > 18) {
        callback(new Error(this.$t('profile.validate.oldPwd.wrong')))
      } else {
        callback()
      }
    }
    const validateNewPwd = (rule, value, callback) => {
      if (value === undefined) {
        callback(new Error(this.$t('profile.validate.newPwd.null')))
      } else if (value.toString().length < 5 || value.toString().length > 18) {
        callback(new Error(this.$t('profile.validate.newPwd.wrong')))
      } else {
        callback()
      }
    }
    const validateRenewPwd = (rule, value, callback) => {
      if (value === undefined) {
        callback(new Error(this.$t('profile.validate.renewPwd.null')))
      } else if (value !== this.resetForm.newPassword) {
        callback(new Error(this.$t('profile.validate.renewPwd.wrong')))
      } else {
        callback()
      }
    }
    return {
      // permission:null,
      dialogPasswordVisible: false,
      dialogUserVisible: false,
      errorPassword: null,

      resetForm: {
        oldPassword: undefined,
        newPassword: undefined,
        renewPassword: undefined
      },
      resetFormRules: {
        oldPassword: [
          { required: true, validator: validateOldPwd, trigger: 'blur' }
        ],
        newPassword: [
          { required: true, validator: validateNewPwd, trigger: 'blur' }
        ],
        renewPassword: [
          { required: true, validator: validateRenewPwd, trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    resetTempPassword() {
      this.resetForm = {
        oldPassword: undefined,
        newPassword: undefined,
        renewPassword: undefined
      }
    },
    resetTemp(obj) {
      Object.getOwnPropertyNames(obj).forEach(function(key) {
        console.log(key + '---' + obj[key])
        obj[key] = undefined
      })
    },
    handleCreatePassword() {
      this.resetTempPassword()
      this.dialogStatus = 'create'
      this.dialogPasswordVisible = true
      this.$nextTick(() => {
        this.$refs['resetForm'].clearValidate()
      })
      console.log(this)
    },
    createPassword() {
      this.$refs['resetForm'].validate(valid => {
        if (valid) {
          this.errorPassword = ''

          changePassword({
            newPassword: this.resetForm.newPassword,
            oldPassword: this.resetForm.oldPassword
          }).then(data => {
            if (data.code === 200) {
              this.dialogPasswordVisible = false
              this.$notify({
                title: 'Success',
                message: this.$t('common.successfullyModified'),
                type: 'success',
                duration: 2000
              })
              console.log(data)
            } else if (data.code === 1022) {
              this.errorPassword = data.msg
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
</style>
